<!-- 正文开始 -->
<div class="layui-fluid" id="sysJobSearch">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-form toolbar" lay-filter="sysJobSearch">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <select name="searchCode">
                            <option value="">搜索条件</option>
                            <option value="jobName">任务名称</option>
                            <option value="jobClass">任务类名</option>
                        </select>
                    </div>
                    <div class="layui-inline">
                        <input name="searchValue" class="layui-input" type="text" placeholder="请输入"/>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn icon-btn btnSearch"><i class="layui-icon">&#xe615;</i>搜索</button>
                        <button class="layui-btn icon-btn btnAdd"><i class="layui-icon">&#xe654;</i>添加</button>
                        <button class="layui-btn icon-btn btnSave layui-btn-normal"><i class="layui-icon layui-icon-ok"></i>保存</button>
                    </div>
                </div>
            </div>

            <table class="layui-table" id="sysJobTable" lay-filter="sysJobTable"></table>

        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="tpl-sysJobTable-operator">
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="start">启动</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="stop">停止</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="forceStop">强行停止</a>
</script>

<!-- 表格状态列 -->
<script type="text/html" id="tpl-sysJobTable-isDone">
    <input disabled type="checkbox" lay-filter="tpl-sysJobTable-status" value="{{d.id}}" lay-skin="switch" lay-text="启动|停止"
           {{d.isDone==0 ? 'checked' : ''}}/>
</script>

<script type="text/html" id="tpl-sysJobTable-setIsStartPlan">
    <input disabled type="checkbox" lay-filter="tpl-sysJobTable-status" value="{{d.id}}" lay-skin="switch" lay-text="运行中|停止"
           {{d.setIsStartPlan==1 ? 'checked' : ''}}/>
</script>

<script type="text/html" id="tpl-sysJobTable-isRun">
    <input disabled type="checkbox" lay-filter="tpl-sysJobTable-status" value="{{d.id}}" lay-skin="switch" lay-text="运行中|停止"
           {{d.isRun==1 ? 'checked' : ''}}/>
</script>

<script type="text/html" id="tpl-sysJobTable-hasError">
    <a data-id="{{d.id}}" class="layui-btn layui-btn-danger layui-btn-xs {{d.hasError==1 ? '' : 'layui-hide'}}" lay-event="hasError">查看</a>
</script>

<script type="text/html" id="tpl-sysJobTable-errorView">
    <pre class="layui-code"></pre>
</script>


<script>
    layui.use(['layer', 'table', 'form', 'http', 'tools','code'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var table = layui.table;
        var form = layui.form;
        var http = layui.http;
        var tools = layui.tools;

        var viewId = "#sysJobSearch";
        var $view = $(viewId);
        var $errorView = $('#tpl-sysJobTable-errorView');

        form.render('select','sysJobSearch');

        //渲染表格
        var sysJobTableIns = table.render({
            elem: '#sysJobTable',
            url: http.base_server + 'sysJob/list',
            where: {
                // access_token: http.getToken().access_token
            },
            page: false,
            cellMinWidth: 100,
            height: 'full-200',
            cols: [[
                {type: 'numbers'},
                {field: 'id', sort: true, title: 'ID', hide:true},
                {field: 'jobName', sort: true, title: '任务名称',width:200, edit: 'text'},
                {field: 'jobClass', sort: true, title: '任务类名',width:200, edit: 'text'},
                {field: 'cron', sort: false, title: '表达公式',width:100, edit: 'text'},
                {field: 'startDate', sort: true, title: '启动',width:150},
                {field: 'stopDate', sort: true, title: '停止',width:150},
                {field: 'setIsStartPlan', sort: false, templet: '#tpl-sysJobTable-isDone', title: '计划',width:80, align:"center"},
                {field: 'isRun', sort: false, templet: '#tpl-sysJobTable-isRun', title: '执行',width:80, align:"center"},
                {align: 'center', toolbar: '#tpl-sysJobTable-operator', title: '操作', width: 160},
                {field: 'hasError', sort: false, templet: '#tpl-sysJobTable-hasError', title: '有错误',width:80, align:"center"},
                {field: 'updateTime', sort: false, title: '更新时间'},
                {field: 'updateByWho', sort: false, title: '更新人'}
            ]],
            error: function(code,msg){
                http.fail(code,msg);
            }
        });

        var fnSearch = function(){
            var param = {};
            var searchCode = $view.find("select[name='searchCode']").val();
            if(!searchCode){
                searchCode='name';
            }
            var searchValue = $view.find("input[name='searchValue']").val();
            if(searchCode && searchCode.length>0){
                param[searchCode] = searchValue;
            }
            sysJobTableIns.reload({where: param});
        }

        // 搜索按钮点击事件
        $view.find(".btnSearch").click(function () {
            fnSearch();
        });
        // 添加按钮点击事件
        $view.find(".btnAdd").click(function () {
            sysJobTableIns.addRow({});
        });


        // 工具条点击事件
        table.on('tool(sysJobTable)', function (rowObj) {
            var data = rowObj.data;
            if (rowObj.event === 'start') { //启动
                http.get('sysJob/start',{id: data.id},function (res) {
                    if(res.ok){
                        http.okMsg(res.msg);
                        fnSearch();
                    }
                })
            } else if (rowObj.event === 'stop') { //停止
                http.get('sysJob/stop',{id: data.id, isForceStop: false},function (res) {
                    if(res.ok){
                        http.okMsg(res.msg);
                        fnSearch();
                    }
                })
            }else if (rowObj.event === 'forceStop') { //强行停止
                http.get('sysJob/stop',{id: data.id, isForceStop: true},function (res) {
                    if(res.ok){
                        http.okMsg(res.msg);
                        fnSearch();
                    }
                })
            }else if (rowObj.event === 'hasError') { //查看错误
                http.get('sysJob/getErrLog',{id: data.id},function (res) {
                    if(res.ok){
                        $errorView.html('<pre class="layui-code">' + res.data||'' + '</pre>');
                        layui.code();
                        layer.open({type:1,content:$errorView.html(),area:['800px','500px']});
                    }
                })
            }
        });

        table.on('edit(sysJobTable)',function (row) {
            var idx = row.tr.attr("data-index");
            var data = {};
            data.id = row.data.id;
            data[row.field] = row.value;
            sysJobTableIns.saveDelta(idx,data);
        });

        $view.find(".btnSave").click(function () {
            var data = sysJobTableIns.getDelta();
            if(!data){return;}
            http.post('sysJob/saveBatch',JSON.stringify(data),function(res){
                if(res.ok){
                    sysJobTableIns.updateCache(res.data);
                    sysJobTableIns.resetDelta();
                    http.okMsg(res.msg);
                }
            });
        });

    });

</script>